rsync stands for remote sync.
rsync is used to perform the backup operation in UNIX / Linux.
rsync utility is used to synchronize the files and directories from one location to another in an effective way. Backup location could be on local server or on remote server.
Important features of rsync
- Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
- Security: rsync allows encryption of data using ssh protocol during transfer.
- Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
- Privileges: No special privileges are required to install and execute rsync
Syntax
$ rsync options source destination
Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.
Example 1. Synchronize Two Directories in a Local Server
To sync two directories in a local computer, use the following rsync -zvr command.
$ rsync -zvr /var/opt/installation/inventory/ /root/temp building file list ... done sva.xml svB.xml . sent 26385 bytes received 1098 bytes 54966.00 bytes/sec total size is 44867 speedup is 1.63 $
In the above rsync example:
- -z is to enable compression
- -v verbose
- -r indicates recursive
Now let us see the timestamp on one of the files that was copied from source to destination. As you see below, rsync didn’t preserve timestamps during sync.
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 bin bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Sep 2 2009 /root/temp/sva.xml
Example 2. Preserve timestamps during Sync using rsync -a
rsync option -a indicates archive mode. -a option does the following,
- Recursive mode
- Preserves symbolic links
- Preserves permissions
- Preserves timestamp
- Preserves owner and group
Now, executing the same command provided in example 1 (But with the rsync option -a) as shown below:
$ rsync -azv /var/opt/installation/inventory/ /root/temp/ building file list ... done ./ sva.xml svB.xml . sent 26499 bytes received 1104 bytes 55206.00 bytes/sec total size is 44867 speedup is 1.63 $
As you see below, rsync preserved timestamps during sync.
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml
Example 3. Synchronize Only One File
To copy only one file, specify the file name to rsync command, as shown below.
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/ Pubkeys sent 42 bytes received 12380 bytes 3549.14 bytes/sec total size is 12288 speedup is 0.99
Example 4. Synchronize Files From Local to Remote
rsync allows you to synchronize files/directories between the local and remote system.
$ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/ Password: building file list ... done ./ rpm/ rpm/Basenames rpm/Conflictname sent 15810261 bytes received 412 bytes 2432411.23 bytes/sec total size is 45305958 speedup is 2.87
While doing synchronization with the remote server, you need to specify username and ip-address of the remote server. You should also specify the destination directory on the remote server. The format is username@machinename:path
As you see above, it asks for password while doing rsync from local to remote server.
Sometimes you don’t want to enter the password while backing up files from local to remote server. For example, If you have a backup shell script, that copies files from local to remote server using rsync, you need the ability to rsync without having to enter the password.
To do that, setup ssh password less login as we explained earlier.
Example 5. Synchronize Files From Remote to Local
When you want to synchronize files from remote to local, specify remote path in source and local path in target as shown below.
$ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames . sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87
Example 6. Remote shell for Synchronization
rsync allows you to specify the remote shell which you want to use. You can use rsync ssh to enable the secured remote connection.
Use rsync -e ssh to specify which remote shell to use. In this case, rsync will use ssh.
$ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87
Example 7. Do Not Overwrite the Modified Files at the Destination
In a typical sync situation, if a file is modified at the destination, we might not want to overwrite the file with the old file from the source.
Use rsync -u option to do exactly that. (i.e do not overwrite a file at the destination, if it is modified). In the following example, the file called Basenames is already modified at the destination. So, it will not be overwritten with rsync -u.
$ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames $ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ sent 122 bytes received 505 bytes 114.00 bytes/sec total size is 45305958 speedup is 72258.31 $ ls -lrt total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames
Example 8. Synchronize only the Directory Tree Structure (not the files)
Use rsync -d option to synchronize only directory tree from source to the destination. The below example, synchronize only directory tree in recursive manner, not the files in the directories.
$ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ . Password: receiving file list ... done logrotate.status CAM/ YaST2/ acpi/ sent 240 bytes received 1830 bytes 318.46 bytes/sec total size is 956 speedup is 0.46
Example 9. View the rsync Progress during Transfer
When you use rsync for backup, you might want to know the progress of the backup. i.e how many files are copies, at what rate it is copying the file, etc.
rsync –progress option displays detailed progress of rsync execution as shown below.
$ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... 19 files to consider ./ Basenames 5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname 12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19) . . . sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec total size is 45305958 speedup is 2.87
You can also use rsnapshot utility (that uses rsync) to backup local linux server, or backup remote linux server.
Example 10. Delete the Files Created at the Target
If a file is not present at the source, but present at the target, you might want to delete the file at the target during rsync.
In that case, use –delete option as shown below. rsync delete option deletes files that are not there in source directory.
# Source and target are in sync. Now creating new file at the target. $ > new-file.txt $ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ . Password: receiving file list ... done deleting new-file.txt ./ sent 26 bytes received 390 bytes 48.94 bytes/sec total size is 45305958 speedup is 108908.55
Target has the new file called new-file.txt, when synchronize with the source with –delete option, it removed the file new-file.txt
Example 11. Do not Create New File at the Target
If you like, you can update (Sync) only the existing files at the target. In case source has new files, which is not there at the target, you can avoid creating these new files at the target. If you want this feature, use –existing option with rsync command.
First, add a new-file.txt at the source.
[/var/lib/rpm ]$ > new-file.txt
Next, execute the rsync from the target.
$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ . root@192.168.1.2's password: receiving file list ... done ./ sent 26 bytes received 419 bytes 46.84 bytes/sec total size is 88551424 speedup is 198991.96
If you see the above output, it didn’t receive the new file new-file.txt
Example 12. View the Changes Between Source and Destination
This option is useful to view the difference in the files or directories between source and destination.
At the source:
$ ls -l /var/lib/rpm -rw-r--r-- 1 root root 5357568 2010-06-24 08:57 Basenames -rw-r--r-- 1 root root 12288 2008-05-28 22:03 Conflictname -rw-r--r-- 1 root root 1179648 2010-06-24 08:57 Dirnames
At the destination:
$ ls -l /root/temp -rw-r--r-- 1 root root 12288 May 28 2008 Conflictname -rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames -rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames
In the above example, between the source and destination, there are two differences. First, owner and group of the file Dirname differs. Next, size differs for the file Basenames.
Now let us see how rsync displays this difference. -i option displays the item changes.
$ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done >f.st.... Basenames .f....og. Dirnames sent 48 bytes received 2182544 bytes 291012.27 bytes/sec total size is 45305958 speedup is 20.76
In the output it displays some 9 letters in front of the file name or directory name indicating the changes.
In our example, the letters in front of the Basenames (and Dirnames) says the following:
> specifies that a file is being transferred to the local host. f represents that it is a file. s represents size changes are there. t represents timestamp changes are there. o owner changed g group changed.
Example 13. Include and Exclude Pattern during File Transfer
rsync allows you to give the pattern you want to include and exclude files or directories while doing synchronization.
$ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Packages Providename Provideversion Pubkeys sent 129 bytes received 10286798 bytes 2285983.78 bytes/sec total size is 32768000 speedup is 3.19
In the above example, it includes only the files or directories starting with ‘P’ (using rsync include) and excludes all other files. (using rsync exclude ‘*’ )
Example 14. Do Not Transfer Large Files
You can tell rsync not to transfer files that are greater than a specific size using rsync –max-size option.
$ rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Conflictname Group Installtid Name Sha1header Sigmd5 Triggername sent 252 bytes received 123081 bytes 18974.31 bytes/sec total size is 45305958 speedup is 367.35
max-size=100K makes rsync to transfer only the files that are less than or equal to 100K. You can indicate M for megabytes and G for gigabytes.
Example 15. Transfer the Whole File
One of the main feature of rsync is that it transfers only the changed block to the destination, instead of sending the whole file.
If network bandwidth is not an issue for you (but CPU is), you can transfer the whole file, using rsync -W option. This will speed-up the rsync process, as it doesn’t have to perform the checksum at the source and destination.
# rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp Password: receiving file list ... done ./ Basenames Conflictname Dirnames Filemd5s Group Installtid Name sent 406 bytes received 15810211 bytes 2874657.64 bytes/sec total size is 45305958 speedup is 2.87
Comments on this entry are closed.
Of the track I recently found one good option for backup (fresh install) of a system which contains lot of customised packages, we can use the “Ghost for linux”. This can be used to take a cold backup of entire system (including the customised packages + OS), which can used to redeploy the same setup in a diffrent server or be used in the same server incase of crash of the system.
This saves the pain of reinstalling the OS and the packages from the begining.
Hi ,
Thanks Its very use full for me
hi folks.
here are two items i always mention when teaching people about ‘rsync’.
i always test it first with a dry run (simulation). append –dry-run or -n to your options to see what files *would* be copied or deleted without actually doing the action.
also, including a trailing slash vs omitting it produces different results.
without the trailing slash…
rsync -avz 192.168.200.10:/tmp/dir .
would produce
dir/file01
dir/file02
whereas including the trailing slash…
rsync -avz 192.168.200.10:/tmp/dir/ .
would produce
file01
file02
keep up the great job on The Geek Stuff! thanks. -mike
What’s the point of compressing data that is only copied from one directory to another on the same computer? (example 1)
EXCELLENT!!!
Mike comment too .)
Great tutorial Ramesh – thanks again for taking the time to put this together!
Hello,
nice tips!
How can I do an incremental backup? I have a folder and want to backup only the modified files…
Thank you!
How to delete file at source after it is copied to destination
Sasikala –> Thanks, very helpful, off to try them out
Mike –> Thanks for your example in your comment (and the dry run option).
Happy Holidays.
hi
my english isnt very well
when we remot to a server by ssh the first directory is the home directory of that user so when we write command like this
rsync -avz -e ssh alieblice@192.168.16.12:var/www home/backup
it says that thers isnt any var/www directory
my question is : how can i backup a folder out of the home directory ?
Well, thanks..
I have learned a lot.. They are useful.. lol
Hi Ali,
Specify this, if you want to do something outside the relative path: rsync -avz -e ssh alieblice@192.168.16.12:/var/www /home/backup
Using absolute paths is a nice way to be 100% sure of what you are copying.
Regards,
Carson.
hi all,
it backup with automatic in one server to another server without password How to anyone help!!!
advance thanks
with regards
Kaling…
thank you very much! i have been trying to use rsync to one-way sync an itunes library from one mac to another machine. is there a way to get more details in to a log file? like date/timestamp? i’ve added >> /path/to/log.txt and it works well. just need to add date/timestamp. also would it be possible to get an email when rsync is done?
Thanks for a great tutorial. in depth and clear.
This is a good summary of rsync stuff that is important.
This was helpful for me!
keep posting stuff like these.
When using the -d option to copy Only the Directory Tree, not only the dir-tree gets copied. FILES in the top source dir also get copied!!
You had better control this with:
rsync -v -d –max-size=’10K’ ~/Music/ .
@diptanu paul
I know it is late!! but this is just to point out that in order to get homogenics workstations we do use at my company the mechanised installation through the Kickstart + PXE boot process. In my opinion it is a lot better than deploying HDD clones, as the KS process is not dependent to the hardware…
just my opinion and my experiences.
nice tutrl………………..thnks alot
Hi Ramesh,
Thanks for your awesome rsync article. I would like to include the 16 rsync example command here below.
Rsync with custom SSH port no :
==========================
# rsync -avz -e “ssh -p 2323” nagios-plugins-1.4.1.tar.gz root@xx.xx.xx.xx:/root
root@xx.xx.xx.xx‘s password:
nagios-plugins-1.4.1.tar.gz
sent 967995 bytes received 42 bytes 45024.98 bytes/sec
total size is 967552 speedup is 1.00
==========================
Realy it’s help me a lots . Thanks buddy.
When running rsync between 2 local directories, you dont want to use -z (compress), it’ll just load up your processor and slow down the transfer.
Thank’s for this very usefull article. Thank’s to you too TK Nallappan, for the ssh tips
Very, very useful. I learnt so much, so quickly thanks to this post. Many thanks.
Is it possible to tell rsync to copy only files belonging to a certain user ?
I have the problem that when I tell rsync to copy links I don’t want to end up
with a lot of files from other people (if the links point there).
Your tutorials are easy to understand for me. Please Keep writing
Very good article, and usefull topics.
U ROCK
Very well written and easy to understand. A+
Thanks!!!!
ssh: connect to host 192.168.3.6 port 22: Connection refused
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [sender=3.0.8]
what is the problem with this?
THANK YOU!
Use ” -v ” to get more data
ynu can use up to 3 “v”
really awesome. even offical site for rsync is not so clear. explained with examples really rocks.
Thanks.
I want to transfer my output data files(vtk/png files) only from remote to local computer. What can I do in this regard?
Excellent tutorial!
# rsync -avz administrator@10.120.1.1:/win7/a /home/siraj/
connection ssh port 22 refused
(i am getting an error when trying to copy from windows to linux
please help)
Great article, and well explained.
I’d like to find some further information on the: “only sending the changed blocks”. I find this amazing in situations where there is a large file– such as a VM file.
Nice article, for bonus points: is there any way to capture all changed files and put them somewhere on each run? I’m using –backup-dir but that only retains one version of each file. Maybe the trick is to gzip that tree and erase it after each run, but I also want to set up so that only the original rsync command can be executed ( have to push from a shared host, want to secure the backup server better ). Basically looking for a version-control-like setup that captures most versions of files in development at least at the intervals I set.
Siraj, you can debug your connection using an interactive ssh. Best thing to do is to use certificates if you want an unattended backup, this isn’t covered in this article but search rsync ssh-keygen to find several good articles on doing those.
Can achieve the following …
updating of a single file locally, should update the remote copy instantly
very useful!!!!!!!!!!!!
Hi, i am using rsync to backup from remote to local but in backup it not recognise the extension of file such as font that not have extension like .ttf etc. in backup copy not reconised as font but it seems a document and i cant use that font from backup. Is something special for risolve this in rsync? Thanks to all if someone can help me.
I have errors at server and can not update.
Repeat calls to rysnc to syncronize, but it deletes file on the destination folder. So an error can delete many gigs of already copied data the next time rsync is called.
Hi ,
its really very useful , please keep it up .. it help us a lot
Regards
Chandan Kumar
Ramesh ,
Can you help me for one rsync scenario . if i removed some source files then how we can refelect this changes on destination using rsync. any help from you and others will be appriciated . Thanks in advance.
Guarav:
If you use the –delete option, it will delete the files on the target, causing a truly synchronized system. IE:
rsync -av –delete /path/to/source /path/to/destination
Thanks,
Joseph for your help !!!
Awesome info and example. I do one question about the -W option. I am rsync on a LAN network work as oppose to the over the wan, should I use the -W option? I don’t see much of -W option any of the other documentations and examples. Just curious. Looking to foward to hear from you.
This is extremely useful. It provides all the information I have been researching for the past several days about syncing from my remote server back to my local machine.
Thanks,
Can u have any steps to take full backup of OS and restore it on another server using live CD.
excellent geekss……good stufff..
Great guide. Useful e simple to read ! thanks a lot.
rsync can use hard links for create incremental backups, but even so some kind of backup rotation is needed to limit the required disk space. Check here.
10 backups gives usually 10 days or retention.
I writed a rsync shellscript with a non-linear rotation to extend this retention.
10 backups is then enough to retain 1 year of the backups, e.g. with daily snapshots 1, 2, 3, 4, 6, 10, 18, 50, 114, 242.
good
Nice article! Very helpful. 🙂
Thank you Mr.Ramesh.
I’ve been using RSYNC (actually GRSYNC) to backup my entire Home directory to an external harddrive for years. Then when it comes time to upgrade to the next release, I tend to install new (instead of allow system to update to the next release) and have used my saved files on my external harddrive to recover my data. It has worked great. The first copy will take awhile, dependent upon the number and size of your files, but after that it will look for what has changed and only copy of those files. Very quick.
Very nicely done
Hi there,
Thanks for the great info! Just wanted to mention though, and correct me if I’m wrong, but I think you’ve got the ls -l examples of backup preserving or not-preserving timestamps the wrong way round.
Cheers again,
Ollie.
************************
Very nicely documented..
Thanks Ramesh for this…
Very nice description. Thanks a bunch buddy!
how to transfer only one particular file when we want it to be transferred to remote system. Can’t use scp/sftp
I still can’t see that big difference between rsync and scp!
Very clearly written, thanks much!
How to copy live data like sql
and block level in rsync ??? can u give me way to do this ???
Great!
i use ssl whith publickey.
how do i use rssync ?
I refer to your website whenever I want to use rsync.
Clear. Concise. Incredibly usable.
Thank you.
Wow, I am amazed at the power of this tool/command.
Thanks.
Very good explanation. I would like to include a link to this page as “useful rsync examples” if you allowed. Keep up the good job.
It is very useful for beginners.